<?php

namespace Admin\Model\DAO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Entity\Book;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;
use Zend\Db\Adapter\Adapter;

class BookStoreTable extends AbstractTableGateway {

    protected $table;
    protected $adapter;
    protected $resultSetPrototype;

    public function __construct(Adapter $adapter) {
        $this->table = 'lib_bookstores';
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
    }

    public function getBookCategory() {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('bookCategoryID', 'bookCategoryName'))
                ->from('lib_bookcategories');
        // you can check your query by echo-ing :
        //  echo $select->getSqlString();
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        return $resultSet;
    }

    public function getBookType() {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('bookTypeID', 'bookTypeName'))
                ->from('lib_booktypes');
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        return $resultSet;
    }

    public function getLanguage() {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->columns(array('languageID', 'languageName'))
                ->from('lib_booklanguages');
        // you can check your query by echo-ing :
        //  echo $select->getSqlString();
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        return $resultSet;
    }

    public function getBookBarcode($bookBarcode) {
        $rowset = $this->select(array('bookBarcode' => $bookBarcode));
        $row = $rowset->current();
        if (!$row)
            return null;
        else
            return $row;
    }

    public function getBookDetail($bookBarcode) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table))
                ->join(array('bc' => 'lib_bookCategories'), 'bs.bookCategoryID = bc.bookCategoryID', array('bookCategoryName'))
                ->join(array('bl' => 'lib_bookLanguages'), 'bl.languageID = bs.languageID', array('languageName'))
                ->join(array('bt' => 'lib_bookTypes'), 'bt.bookTypeID = bs.bookTypeID', array('bookTypeName'));
        $where = new Where();
        $where->equalTo('bs.bookBarcode', $bookBarcode);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }
        public function getInfoBook($bookBarcode) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table))
                ->join(array('bc' => 'lib_bookCategories'), 'bs.bookCategoryID = bc.bookCategoryID', array('bookCategoryName'))
                ->join(array('bl' => 'lib_bookLanguages'), 'bl.languageID = bs.languageID', array('languageName'))
                ->join(array('bt' => 'lib_bookTypes'), 'bt.bookTypeID = bs.bookTypeID', array('bookTypeName'));
        $where = new Where();
        $where->equalTo('bs.bookBarcode', $bookBarcode);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $row = $result->current();
        return $row;
    }
    public function searchBookBarcode($bookBarcode) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table));
        $where = new Where();
        $where->equalTo('bs.bookBarcode', $bookBarcode);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function searchBookName($valueSearch = null) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table));
        $where = new Where();
        $where->like('bs.title', '%' . $valueSearch . '%');
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function searchBookPublisher($valueSearch = null) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table));
        $where = new Where();
        $where->like('bs.publisher', '%' . $valueSearch . '%');
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function searchBookCategory($valueSearch = null) {
        $sql = new Sql($this->adapter);
        $select = $sql->select();
        $select->from(array('bs' => $this->table));
        $where = new Where();
        $where->equalTo('bs.bookCategoryID', $valueSearch);
        $select->where($where);
        $statement = $sql->prepareStatementForSqlObject($select);
        $result = $statement->execute();
        $resultSet = clone $this->resultSetPrototype;
        $resultSet->initialize($result);
        $resultSet->buffer();
        $resultSet->next();
        return $resultSet;
    }

    public function saveBook(Book $book) {
        $data = $book->getArrayCopy();
        try {
            $this->insert($data);
            return true;
        } catch (Exception $e) {
            echo "loi insert book roi";
        }
        return false;
    }
    public function updateBookInfo($data, $bookBarcode) {
        $this->update($data, array('bookBarcode' => $bookBarcode));
    }
}

?>
